<template>
  <div>
    <el-tree
      ref="refTree"
      node-key="id"
      :data="list"
      :props="{label:'name'}"
      show-checkbox
      default-expand-all
      check-strictly
    />
    <div>
      <el-button @click="hCancel">取消</el-button>
      <el-button @click="hSave">确定</el-button>
    </div>
  </div>
</template>
<script>
import { getPermission } from '@/api/permissions'
import { getRolesById, getRolesAssign } from '@/api/settings'
import { recursive } from '@/utils/index'
export default {
  props: {
    curId: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadGetPermission()
    this.loadgetRolesById()
  },
  methods: {
    async loadGetPermission() {
      const res = await getPermission()
      this.list = recursive(res.data, '0')
    },
    hCancel() {
      this.$emit('cancel')
    },
    async loadgetRolesById() {
      console.log(this.curId)
      const res = await getRolesById(this.curId)
      this.$refs.refTree.setCheckedKeys(res.data.permIds)
    },
    async hSave() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      await getRolesAssign({ id: this.curId, permIds: permIds })
      this.$message.success('保存角色权限成功')
      this.$emit('cancel')
    }
  }
}
</script>
